#include "httppixmapform.h"

#include "ui_httppixmapform.h"

#include <QNetworkReply>
#include <QSslSocket>
#include <http/DownloadFile.h>
#include <memory>
#include <qnetworkrequest.h>

HttpPixmapForm::HttpPixmapForm(QWidget* parent) : QWidget(parent),
                                                  ui(new Ui::HttpPixmapForm) {
    ui->setupUi(this);
    qDebug() << QSslSocket::supportsSsl() << QSslSocket::sslLibraryBuildVersionString();
    // connect(&manager, &QNetworkAccessManager::finished, this, &HttpPixmapForm::on_finished);
}

HttpPixmapForm::~HttpPixmapForm() {
    delete ui;
}

void HttpPixmapForm::on_finished(QNetworkReply* reply) {
    QPixmap pixmap;
    pixmap.loadFromData(reply->readAll());
    ui->label_pixmap->setPixmap(pixmap);
}

void HttpPixmapForm::on_btn_get_clicked() {
    QString url = ui->lineEdit->text();
    if (url.mid(0, 4) != "http") {
        url.push_front("http://");
    }

    DownloadFile* downloadfile = new DownloadFile();

    connect(downloadfile, &DownloadFile::finished, [=](std::shared_ptr<QByteArray> data) {
        QPixmap pixmap;
        pixmap.loadFromData(*data);
        ui->label_pixmap->setPixmap(pixmap);
        downloadfile->deleteLater();
    });
    downloadfile->download(url);
}
